package com.youtui.manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.youtui.common.constant.ResultMessage;
import com.youtui.common.util.FileUploadUtil;
import com.youtui.common.util.ResultUtil;

@Controller
@RequestMapping("/file")
public class FileUploadController extends BaseController {

	private @Autowired FileUploadUtil fileUploadUtil;
 
	/**
	 * 文件上传
	 * 
	 * @param uploadfile
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	@RequestMapping({ "/upload.do" })
	@ResponseBody
	public ResultMessage upload(@RequestParam("file") String file /* 文件内容base64 */,
			@RequestParam(value = "fileName", required = false , defaultValue =".jpg") String fileName/* 文件名称 */) throws Exception {
		String localPath = request.getSession().getServletContext().getRealPath("/");
		logger.debug(localPath);
		//http://localhost/youtui/file/upload.do
		logger.debug(request.getRequestURL().toString());
		logger.debug(request.getRequestURI());
		String base_server_url=request.getRequestURL().toString().replaceAll(request.getRequestURI(), "");
		localPath = localPath.replace("youtui", "files");
		localPath = localPath.replace("ROOT", "files");
		logger.debug(localPath);
		if (file == null) {
			return ResultUtil.result(ResultUtil.code_fail, "文件信息不存在");
		}
		String result = fileUploadUtil.GenerateImage(localPath, file.split("base64,")[1], fileName);
		return ResultUtil.result(ResultUtil.code_success, "文件上传成功", base_server_url+"/files/"+result);
	}

}
